********************************************************************************
*
**#		Table 1: Randomnesss of Assignment
*			
********************************************************************************

use "${DATA}/data_DV.dta", clear

cap erase "${OUTPUT}/tab1.xlsx"

********************************************************************************
*
**##	Table 1.A:	DV Cases
*
********************************************************************************

# d ;

matrix drop _all ;

local charac "white age days_arrest_court charge_count prev_court prev_court_num
			  prev_court_dv prev_court_aslt oca_record_max" ;

local m = 3	;
local clab  = `m' - 1 ;	/* column label */
local pnl 	= `m' - 2 ; /* panel */


putexcel set "${OUTPUT}/tab1", sheet("Table1") replace ;

	qui putexcel A`clab' = "Defendant Characteristics"	;
	qui putexcel B`clab' = "Uncod Mean General Court"	;
	qui putexcel C`clab' = "Specialized Court Predicted Value" 	;
	qui putexcel D`clab' = "p-value" 	;

foreach x of local charac { ;					 
	
	local lhs_lbl : variable label `x' 			;	
	reghdfe `x' dv_judge if dv_case == 1, $hdfe_opts ;
		matrix B 	=r(table) 					;
		local beta	=round(B[1,1], 0.001)		;
		local pval	=string(B[4,1], "%9.2f") 	;	
		
	ttest  `x' if e(sample)==1, by(dv_judge)		;
		local mean0	=round(r(mu_1), 0.001)			; 
		local pred 	=round(`mean0' + `beta',0.001)  ;

		local general_N =r(N_1) 					;
		local dv_N 		=r(N_2) 					;
		local total_N 	=`general_N'+`dv_N' 		;	
		
	reghdfe dv_judge `charac' if dv_case == 1, $hdfe_opts ;
		test `charac' 								;
		local ftest	=string(`r(F)', "%9.2f")		; 
		local fpval	=string(`r(p)', "%9.2f")		;
		
	putexcel set "${OUTPUT}/tab1", sheet("Table1") modify ;
		qui putexcel A`m' 	= "`lhs_lbl'" 	;
		qui putexcel B`m' 	= `mean0'		;
		qui putexcel C`m' 	= `pred'		;
		qui putexcel D`m' 	= "(`pval')"	;
		qui putexcel A`pnl' = "Panel A: DV Cases 		[ N = `total_N' ]" ;

local m `++m' ;
dis `m' ;
} ;

		qui putexcel A`m' = "N"			;
		qui putexcel B`m' = `general_N'	;
		qui putexcel C`m' = `dv_N'		;
		
		local m `++m' ;
		
		qui putexcel A`m' = "F-test of joint significance (p-value)" ;
		qui putexcel D`m' = "`ftest' (`fpval')"		;

# d cr

********************************************************************************
*
**##	Table 1.B :  Other Cases 
*
********************************************************************************

# d ; 

matrix drop _all	;
local m = `m'+4 	;

local clab  = `m' - 1 ;	/* column label */ 
local pnl 	= `m' - 2 ; /* panel */ 


putexcel set "${OUTPUT}/tab1", sheet("Table1") modify 			;
	qui putexcel A`clab' = "Defendant Characteristics"			;
	qui putexcel B`clab' = "Uncod Mean General Court"			;
	qui putexcel C`clab' = "Specialized Court Predicted Value" 	;
	qui putexcel D`clab' = "p-value" 							;
	
foreach x of local charac { ;					 
	
	local lhs_lbl : variable label `x' 			;	
	reghdfe `x' dv_judge if dv_case == 0, $hdfe_opts ;
		matrix B 	=r(table) 					;
		local beta	=round(B[1,1], 0.001) 		;
		local pval	=string(B[4,1], "%9.2f") 	;	
		
	ttest  `x' if e(sample)==1, by(dv_judge)		;
		local mean0	=round(r(mu_1), 0.001)			; 
		local pred 	=round(`mean0' + `beta',0.001)  ;

		local general_N =r(N_1) 			;
		local dv_N 		=r(N_2) 			;
		local total_N 	=`general_N'+`dv_N' ;	
		
	reghdfe dv_judge `charac'  if dv_case == 0, $hdfe_opts 	;
		test `charac' 									;
		local ftest	=string(`r(F)', "%9.2f")			; 
		local fpval	=string(`r(p)', "%9.2f")			;
		
	putexcel set "${OUTPUT}/tab1", sheet("Table1") modify ;
		qui putexcel A`m' 	= "`lhs_lbl'" 	;
		qui putexcel B`m' 	= `mean0'		;
		qui putexcel C`m' 	= `pred'		;
		qui putexcel D`m'	= "(`pval')"	;
		qui putexcel A`pnl' = "Panel B: Other Cases 		[ n = `total_N' ]" ;

local m `++m' ;
} ;

		qui putexcel A`m' = "N"			;
		qui putexcel B`m' = `general_N'	;
		qui putexcel C`m' = `dv_N'		;

		local m `++m' ;
		
		qui putexcel A`m' = "F-test of joint significance (p-value)" ;
		qui putexcel D`m' = "`ftest' (`fpval')"		;
		
	

# d cr

********************************************************************************
*   End of File
********************************************************************************
